package bookmanage;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

public class CodeInsert extends JInternalFrame {
	private JLabel lblNewLabel;
	private JTextField code1;
	private JComboBox comboBox;
	private JButton btnNewButton;
	private JLabel label;
	private JComboBox comboBox_1;
	private JTextField code2;
	private JButton button;

	Connection con;

	public CodeInsert(Connection con) {
		super("코드 등록",true,true,false,true);
		this.con=con;
		setResizable(false);
		setBounds(100, 100, 355, 170);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getCode1());
		getContentPane().add(getComboBox());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getLabel());
		getContentPane().add(getComboBox_1());
		getContentPane().add(getCode2());
		getContentPane().add(getButton());

		try {
			String sql="select big_name from codetbl where small_name is null";
			PreparedStatement psmt = con.prepareStatement(sql);
			ResultSet rs = psmt.executeQuery();
			while(rs.next()){
				comboBox.addItem(rs.getString(1));
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("대분류");
			lblNewLabel.setBounds(12, 10, 70, 23);
		}
		return lblNewLabel;
	}
	private JTextField getCode1() {
		if (code1 == null) {
			code1 = new JTextField();
			code1.setBounds(87, 42, 116, 24);
			code1.setColumns(10);
		}
		return code1;
	}
	private JComboBox getComboBox() {
		if (comboBox == null) {
			comboBox = new JComboBox();
			comboBox.addItem("선택");
			comboBox.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					String c1=(String)comboBox.getSelectedItem();
					comboBox_1.removeAllItems();
					comboBox_1.addItem("선택");
					try {
						String sql = "select small_name from codetbl where big_name=? and small_name is not null";
						PreparedStatement psmt = con.prepareStatement(sql);
						psmt.setString(1, c1);
						ResultSet rs = psmt.executeQuery();
						while(rs.next()){
							comboBox_1.addItem(rs.getString(1));
						}
					} catch (Exception e2) {
						e2.printStackTrace();
					}
					
				}
			});
			comboBox.setBounds(87, 11, 116, 24);
		}
		return comboBox;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("추가");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String c1=code1.getText().trim();
					if(c1.equals("")){
						JOptionPane.showMessageDialog(null, "추가하실 대분류명을 입력해주세요!");
						return;
					}

					try {
						String sql = "select count(*) from codetbl where big_name=?";
						PreparedStatement psmt = con.prepareStatement(sql);
						psmt.setString(1, c1);
						ResultSet rs = psmt.executeQuery();
						int cnt=0;
						if(rs.next()){
							cnt = rs.getInt(1);
						}
						if(cnt==0){
							sql="insert into codetbl(code_no,big_name) values(seq_code_no.nextval,?)";
							psmt = con.prepareStatement(sql);
							psmt.setString(1, c1);
							int ct = psmt.executeUpdate();
							if(ct==1){
								JOptionPane.showMessageDialog(null, c1+"이 대분류 코드로 추가 되었습니다!");
								code1.setText("");
								code1.requestFocus();
								comboBox.addItem(c1);
								return;
							}
						}else{
							JOptionPane.showMessageDialog(null, "이미 등록된 코드 입니다!");
							code1.requestFocus();
							return;
						}
					} catch (Exception e2) {
						e2.printStackTrace();
					}
				}
			});
			btnNewButton.setBounds(230, 41, 97, 23);
		}
		return btnNewButton;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("소분류");
			label.setBounds(12, 73, 70, 23);
		}
		return label;
	}
	private JComboBox getComboBox_1() {
		if (comboBox_1 == null) {
			comboBox_1 = new JComboBox();
			comboBox_1.addItem("선택");
			comboBox_1.setBounds(87, 74, 116, 24);
		}
		return comboBox_1;
	}
	private JTextField getCode2() {
		if (code2 == null) {
			code2 = new JTextField();
			code2.setColumns(10);
			code2.setBounds(87, 106, 116, 24);
		}
		return code2;
	}
	private JButton getButton() {
		if (button == null) {
			button = new JButton("추가");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int idx = comboBox.getSelectedIndex();
					if(idx>0){
						String c1 = (String)comboBox.getItemAt(idx);
						String c2 = code2.getText().trim();
						if(c2.equals("")){
							JOptionPane.showMessageDialog(null, "추가하실 소분류명은 입력해주세요!");
							code2.requestFocus();
							return;
						}

						try {
							String sql ="select count(*) from codetbl where big_name=? and small_name=?";
							PreparedStatement psmt = con.prepareStatement(sql);
							psmt.setString(1, c1);
							psmt.setString(2, c2);
							ResultSet rs = psmt.executeQuery();
							int cnt=0;
							if(rs.next()){
								cnt=rs.getInt(1);
							}
							if(cnt==0){
								sql="insert into codetbl values(seq_code_no.nextval,?,?)";
								psmt = con.prepareStatement(sql);
								psmt.setString(1, c1);
								psmt.setString(2, c2);
								int ct = psmt.executeUpdate();
								if(ct==1){
									JOptionPane.showMessageDialog(null, c2+"가 "+c1+"의 소분류 코드로 등록 되었습니다!");
									code2.setText("");
									code2.requestFocus();
									comboBox_1.addItem(c2);
									return;
								}
							}else{
								JOptionPane.showMessageDialog(null, "이미 등록된 코드 입니다!");
								code2.requestFocus();
								return;
							}
						} catch (Exception e2) {
							e2.printStackTrace();
						}

					}else{
						JOptionPane.showMessageDialog(null, "대분류 코드를 선택해주세요!");
					}
				}
			});
			button.setBounds(230, 105, 97, 23);
		}
		return button;
	}
}
